package com.cf.studiomanage.controller;

import com.cf.studiomanage.entity.Finance;
import com.cf.studiomanage.service.FinanceService;
import com.cf.studiomanage.util.R;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/finance")
@Tag(name = "财务模块", description = "财务收支记录接口")
public class FinanceController {

    @Autowired
    private FinanceService service;

    @PostMapping("/add")
    @Operation(summary = "添加财务记录", description = "添加收入或支出记录")
    public R add(@RequestBody Finance finance) {
        return service.addRecord(finance) ? R.ok("添加成功") : R.error("添加失败");
    }

    @GetMapping("/list")
    @Operation(summary = "查看所有记录", description = "管理员查看全部财务流水")
    public R getAll() {
        return R.ok().data("list", service.getAll());
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查看用户记录", description = "根据用户 ID 查看个人财务记录")
    public R getByUser(@PathVariable Long userId) {
        return R.ok().data("list", service.getByUser(userId));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除记录", description = "根据 ID 删除财务记录")
    public R delete(@PathVariable Long id) {
        return service.deleteById(id) ? R.ok("删除成功") : R.error("删除失败");
    }
}
